package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandService categoryBrandService;


    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> ids) {

        return BrandDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public PageDTO<BrandDTO> pageQuery(Integer page, Integer rows, String key) {

        Page<Brand> brandPage = new Page<>(page,rows);
        //select * from tb_brand [where name lick %key% or letter = key] limit 123,456
        page(brandPage,new QueryWrapper<Brand>()
                .like(null!=key,"name",key)
                .or()
                .eq(null!=key,"letter",key)
        );
        return new PageDTO(brandPage.getTotal(),brandPage.getPages(),BrandDTO.convertEntityList(brandPage.getRecords()));
    }

    @Override
    public List<BrandDTO> queryBrandByCategory(Long cid) {

        List<Long> brandIds = this.categoryBrandService
                .query()
                .eq("category_id", cid)
                .list()
                .stream()
                .map(CategoryBrand::getBrandId)
                .collect(Collectors.toList());
        return queryBrandByIds(brandIds);
    }

    @Override
    @Transactional
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {

        Brand brand = brandDTO.toEntity(Brand.class);

        //保存品牌并主键回显
        if (save(brand)){
            addCategoryAndBrand(cids, brand.getId());

        }

    }

    private void addCategoryAndBrand(List<Long> cids, Long bid) {


        //根据分类的id集合合成中间表关系对象集合
        List<CategoryBrand> categoryBrands = cids
                .stream()
                .map(cid -> CategoryBrand.of(cid, bid))
                .collect(Collectors.toList());

        this.categoryBrandService.saveBatch(categoryBrands);
    }

    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {

        Brand brand = brandDTO.toEntity(Brand.class);

        //品牌修改成功，再去修改中间表
        if (this.updateById(brand)){
            //直接删除关系表，然后重建关系
            deleteCatgegoryAndBrandRela(brand.getId());

            //重建关系
            addCategoryAndBrand(cids,brand.getId());

        }

    }

    private void deleteCatgegoryAndBrandRela(Long bid) {
        //delete from tb_category_brand where brand_id = #{brand.id}
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",bid));
    }

    @Override
    @Transactional
    public void deleteBrand(Long bid) {
        //先删除品牌
        removeById(bid);
        //删除中间表
        deleteCatgegoryAndBrandRela(bid);
    }
}
